﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class PersonCenter : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblUserName.Text = ((Entity.UserInfo)Session["UserInfo"]).UserName;
            if (((Entity.UserInfo)Session["UserInfo"]).UserType == "User")
            {
                lblemail.Text = SQLHelper.getUserEmailByUserAccountId(((Entity.UserInfo)Session["UserInfo"]).UserId);
            }
            else
            {
                lblemail.Text = SQLHelper.getSchoolEmailByAccountId(((Entity.UserInfo)Session["UserInfo"]).UserId);
            }
            btnChangeEmail.Attributes.Add("OnClick", "return testEmailInput();");
            btnChangePassword.Attributes.Add("OnClick", "return testPasswordInput();");
            InitFavouriteList();
        }

        protected void btnChangeEmail_Click(object sender, EventArgs e)
        {
            if (((Entity.UserInfo)Session["UserInfo"]).UserType == "User")
            {
                int iresult = SQLHelper.ChangeUserAccountEmailById(((Entity.UserInfo)Session["UserInfo"]).UserId, tbxchangeemail.Text);
                if (iresult != 1)
                    Response.Write("<script>alert('修改失败!');location.href='PersonCenter.aspx';</script>");
                else
                    Response.Write("<script>alert('修改成功!');location.href='PersonCenter.aspx';</script>");
            }
            else
            {
                int iresult = SQLHelper.ChangeSchoolAccountEmailById(((Entity.UserInfo)Session["UserInfo"]).UserId, tbxchangeemail.Text);
                if (iresult != 1)
                    Response.Write("<script>alert('修改失败!');location.href='PersonCenter.aspx';</script>");
                else
                    Response.Write("<script>alert('修改成功!');location.href='PersonCenter.aspx';</script>");
            }
        }

        protected void btnChangePassword_Click(object sender, EventArgs e)
        {
            int iresult = SQLHelper.ChangeAccountPasswordById(((Entity.UserInfo)Session["UserInfo"]).UserId, tbxPassword.Text);
            if (iresult != 1)
                Response.Write("<script>alert('修改失败!');location.href='PersonCenter.aspx';</script>");
            else
                Response.Write("<script>alert('修改成功!');location.href='PersonCenter.aspx';</script>");
        }
        protected void CancelFavourite_Command(object sender, CommandEventArgs e)
        {
            int iresult = SQLHelper.DeleteFavouriteSchool(((Entity.UserInfo)Session["UserInfo"]).UserId, Convert.ToInt32(e.CommandArgument));
            if (iresult != 1)
                Response.Write("<script>alert('取消失败!');location.href='PersonCenter.aspx';</script>");
            else
                Response.Write("<script>alert('取消成功!');location.href='PersonCenter.aspx';</script>");
        }
        private void InitFavouriteList()
        {
            DataTable dt = new DataTable();
            SQLHelper.GetFavouriteSchoolsByAccountId(((Entity.UserInfo)Session["UserInfo"]).UserId, out dt);
            RepeaterFavourite.DataSource = dt;
            RepeaterFavourite.DataBind();

        }
    }
}